﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Navigation;
using CuteRibs.MVVMLib;
using CuteRibs.WowArmory.Models;
using CuteRibs.WowArmory.Resources;

namespace CuteRibs.WowArmory.ViewModels
{
	public class RealmSearchPageViewModel : PageViewModelBase
	{
		private static Realm[] realmStatus;
		private List<RealmViewModel> markedRealms;
		private List<RealmViewModel> allRealms;

		public List<RealmViewModel> MarkedRealms
		{
			get { return this.markedRealms; }
			set
			{
				if (this.markedRealms != value)
				{
					this.markedRealms = value;
					this.NotifyPropertyChanged("MarkedRealms");
				}
			}
		}

		public List<RealmViewModel> AllRealms
		{
			get { return this.allRealms; }
			set
			{
				if (this.allRealms != value)
				{
					this.allRealms = value;
					this.NotifyPropertyChanged("AllRealms");
				}
			}
		}

		public AppRes AppRes { get { return App.AppResource; } }
		public AppTitleControlViewModel AppTitle { get; set; }
		public string Term { get; set; }
		private int viewIndex;

		public RealmSearchPageViewModel()
		{
			this.AppTitle = new AppTitleControlViewModel(AppRes.Title_Realms, App.RegionData.Region.DisplayName);

			if (App.IsDesignMode)
			{
				this.LoadMarkedRealms();
			}
		}

		public override void OnNavigatedTo(ViewBase sender, NavigationEventArgs eventArgs, NavigationContext context)
		{
			base.OnNavigatedTo(sender, eventArgs, context);

			this.LoadMarkedRealms();
		}

		private void RefreshData()
		{
			if (this.viewIndex == 0)
			{
				this.LoadMarkedRealms();
			}
			else
			{
				this.LoadAllRealms();
			}
		}

		private void LoadMarkedRealms()
		{
			this.BeginInvoke(() =>
			{
				this.ShowProgressBar = true;

				if (string.IsNullOrWhiteSpace(this.Term))
				{
					this.MarkedRealms = App.RegionData.Realms.Select(r => new RealmViewModel(r, true)).ToList();
				}
				else
				{
					this.MarkedRealms = App.RegionData.Realms.Where(r => r.Name.ToUpper().Contains(this.Term.ToUpper())).Select(r => new RealmViewModel(r, true)).ToList();
				}

				this.ShowProgressBar = false;
			});
		}

		private async void LoadAllRealms()
		{
			this.ShowProgressBar = true;

			if (realmStatus == null)
			{
				try
				{
					realmStatus = await App.GetRealms();
				}
				catch (Exception ex)
				{
					App.ShowError(AppException.CreateException(ex, App.RegionData.Region.Name));
				}
			}

			if (string.IsNullOrWhiteSpace(this.Term))
			{
				this.AllRealms = realmStatus.Select(r => new RealmViewModel(r)).ToList();
			}
			else
			{
				this.AllRealms = realmStatus.Where(r => r.Name.ToUpper().Contains(this.Term.ToUpper())).Select(r => new RealmViewModel(r)).ToList();
			}

			foreach (var realm in this.AllRealms.Where(m => App.RegionData.Realms.Any(r => r.Slug == m.Realm.Slug)))
			{
				realm.SetMarked();
			}

			this.ShowProgressBar = false;
		}

		public void FilterRealms(string term = null)
		{
			this.Term = term;
			this.RefreshData();
		}

		public void SwitchView(int index)
		{
			this.viewIndex = index;
			this.RefreshData();
		}

		public void AddRealm(RealmViewModel vm)
		{
			if (!App.RegionData.Realms.Any(r => r.Slug == vm.Realm.Slug))
			{
				App.RegionData.Realms.Add(vm.Realm);
				vm.SetMarked();
			}
		}

		public void DeleteRealm(string slug)
		{
			this.BeginInvoke(delegate
			{
				var realm = App.RegionData.Realms.FirstOrDefault(r => r.Slug == slug);

				if (realm != null)
				{
					App.RegionData.Realms.Remove(realm);
					App.SaveMyData();
				}
			});
		}
	}
}
